package com.roy.comet.parse;

import org.springframework.util.StringUtils;

/**
 * Created by mc on 18/4/13.
 */
public class CometSqlBean {

    public enum SqlType {
        SELECT, INSERT, UPDATE
    };

    private SqlType sqlType;

    private String dbName;
    private String tableName;

    private String sql;

    public SqlType getSqlType() {
        return sqlType;
    }

    public void setSqlType(SqlType sqlType) {
        this.sqlType = sqlType;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String getSplitSql(int dbNo, int tableNo) {
        if (sqlType == null
                || StringUtils.isEmpty(tableName))
            throw new IllegalStateException(
                    "The split SQL should be constructed after the SQL is parsed completely.");

        StringBuffer sb = new StringBuffer();
        /*sb.append(dbName).append("_").append(dbNo);
        sb.append(".");*/
        sb.append(tableName).append("_").append(tableNo).append(" ");
        String newSql = sql.replace(tableName.toLowerCase(),sb.toString());
        return newSql.toString();
    }

    public static void main(String[] args) {
        //select * from user where id = ?
        CometSqlBean cometSqlBean = new CometSqlBean();
        cometSqlBean.setSql("select * from user where id = ?");
        cometSqlBean.setDbName("famliy_show");
        cometSqlBean.setTableName("user");
        cometSqlBean.setSqlType(SqlType.SELECT);
        System.out.print(cometSqlBean.getSplitSql(0,0));
    }
}
